<%@ page import="org.springframework.web.context.request.SessionScope" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%request.setAttribute("ctx", request.getContextPath());%>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layui-v2.9.14/css/layui.css">
    <script src="${pageContext.request.contextPath}/layui-v2.9.14/jquery-2.1.0.min.js"></script>
    <script src="${pageContext.request.contextPath}/layui-v2.9.14/layui.js"></script>
    <script src="${pageContext.request.contextPath}/layui-v2.9.14/layui_ext/xmSelect/xm-select.js"></script>
    <script src="${pageContext.request.contextPath}/layui-v2.9.14/crypto-js.js"></script>
    <title>用户列表</title>
</head>
<body>
<form class="layui-form layui-row layui-col-space16">
    <div class="layui-col-md4">
        <div class="layui-input-wrap">
            <div class="layui-input-prefix">
                <i class="layui-icon layui-icon-username"></i>
            </div>
            <input type="text" name="plateNumber" value="" placeholder="车牌号" class="layui-input" lay-affix="clear">
        </div>
    </div>
    <div class="layui-col-md4">
        <div class="layui-input-wrap">
            <select name="plateTypeId">
                <option value="">车牌类型</option>

            </select>
        </div>
    </div>

    <div class="layui-btn-container layui-col-xs12">
        <button class="layui-btn" lay-submit lay-filter="demo-table-search">Search</button>
        <button type="reset" class="layui-btn layui-btn-primary">Clear</button>
    </div>
</form>
<script type="text/html" id="toolDemo">
    <div class="layui-clear-space">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-xs" lay-event="del">删除</a>

    </div>
</script>
<button type="button" class="layui-btn layui-bg-blue" onclick="addCar()">添加车辆</button>
<button class="layui-btn" lay-on="e1">批量删除</button>
<table class="layui-hide" id="ID-table-demo-search"></table>
<script>

    function addCar(){
        location.href="/car/toadd.do";
    }

    layui.use(function () {
        var table = layui.table;
        var form = layui.form;
        var laydate = layui.laydate;
        var util = layui.util;
        //  2.9+ 版本可省略 attr 参数，默认读取 lay-on
        util.on({
            e1: function(){//批量删除
                //获取要删除的选中的行id
                var checkStatus = table.checkStatus("ID-table-demo-search")
                var data = checkStatus.data; // 获取选中的数据
                //遍历选中的行对象,拼接行id
              let ids=  data.map(function (item){
                    return item.id;
                }).join(",")
              if(ids.length==0){

                  layer.msg("至少选择一个数据",{time:1000})
                  return ;
              }
                layer.confirm('确定删除吗?', function(index){
                   $.post("/car/batchDel.do",{"ids":ids},function (res){
                       if(res.code==0) {
                           layer.msg(res.msg, {time: 2000}, function () {
                             location.href="/car/tolist.do"

                           })
                       }else{
                           layer.msg(res.msg, {time: 2000})
                       }
                   })
                    layer.close(index);
                });

            },
            e2: function(){
                layer.msg('触发了事件 2');
            }
        })
        // 触发单元格工具事件
        table.on('tool(ID-table-demo-search)', function (obj) { // 双击 toolDouble
            var data = obj.data; // 获得当前行数据

            if (obj.event === 'edit') {
                location.href = "/car/toedit.do?id=" + data.id;
            }else  if (obj.event === 'del') {
                location.href = "/car/del.do?id=" + data.id;
            }
        })


       //获取所有车牌分类
        $.get("/car/listPlateType.do", function (res) {
            let data = res.data;
            for (let i = 0; i < data.length; i++) {
                $("[name='plateTypeId']").append("<option value='" + data[i].id + "'>" + data[i].color  + data[i].textColor+ "</option>");
            }
            form.render('select');//渲染下拉框
        })


        // 创建表格实例
        table.render({
            elem: '#ID-table-demo-search',
            url: '/car/list.do', // 此处为静态模拟数据，实际使用时需换成真实接口
            parseData: function (res) {
                return {
                    code: res.code,
                    data: res.data.list,
                    count: res.data.total,
                }
            }, limit: 3,
            cols: [[
                {type: 'checkbox', title: '😊', fixed: true}, // 单选框

                {field: 'plateNumber', title: '车牌号码', width: 180},
                {field: 'cardType', title: '月卡类型', width: 180, sort: true},
                {field: 'isActive', title: '月卡状态', width: 180},
                {field: 'days', title: '剩余有效期', width: 180},
                {field: 'ownerName', title: '车主姓名', width: 180},

                {field: 'ownerPhone', title: '手机号码', width: 180},
                {field: 'parkNum', title: '车位号', width: 180},
                {field: 'createdTime', title: '办理时间', width: 180},

                {fixed: 'right', title: '操作', width: 134, minWidth: 125, templet: '#toolDemo'}
            ]],
            page: true,
            height: 310
        });

        // 日期
        laydate.render({
            elem: '.demo-table-search-date'
        });
        // 搜索提交
        form.on('submit(demo-table-search)', function (data) {
            var field = data.field; // 获得表单字段
            // 执行搜索重载
            table.reload('ID-table-demo-search', {
                page: {
                    curr: 1 // 重新从第 1 页开始
                },
                where: field // 搜索的字段
            });
            layer.msg('搜索成功');
            return false; // 阻止默认 form 跳转
        });
    });
</script>

</body>
</html>